Connect to Android using SSH and Termux
This guide explains how to connect to your Android device from a Linux desktop using SSH (Secure Shell), with the help of the Termux app. SSH allows you to securely access your phone's command-line environment remotely from another machine on the same network.
Step 1: Install and Configure SSH in Termux
-
Open the Termux app on your Android phone.
-
Update and upgrade Termux packages:
pkg update && pkg upgrade -
Install the OpenSSH package:
pkg install openssh -
Start the SSH daemon:
sshdTo stop the server later, run:
pkill sshd
Step 2: Find Your Android Device's IP Address
-
Make sure your Android phone and Linux desktop are connected to the same Wi-Fi network.
-
In Termux, run:
ip address -
Look for the
inetaddress under thewlan0interface (typically something like192.168.x.xor10.0.x.x).
Step 3: Connect to Your Phone from a Linux Desktop
-
On your Linux desktop, open a terminal.
-
Connect to the Android device using:
ssh -p 8022 username@192.168.x.xReplace
usernamewith the output of thewhoamicommand from Termux (usuallyu0_aXXX) or set a password using:passwd -
Enter the Termux password when prompted.
Optional: Set Up SSH Key-Based Authentication
To avoid entering your password each time and improve security, set up SSH key-based authentication.
Generate an SSH Key Pair on Linux
Run the following command on your Linux desktop:
ssh-keygen -t ed25519
Alternatively:
ssh-keygen -t rsa -b 4096
- Accept the default file location.
- Choose whether to set a passphrase.
This creates two files in ~/.ssh/:
id_ed25519orid_rsa: private keyid_ed25519.puborid_rsa.pub: public key
Copy the Public Key to Your Android Device
Run the following command on your desktop:
ssh-copy-id -p 8022 username@192.168.x.x
If ssh-copy-id fails, you can do it manually:
-
On your Linux desktop:
cat ~/.ssh/id_ed25519.pub -
Copy the key to clipboard.
-
Connect to Termux:
ssh -p 8022 username@192.168.x.x -
Inside Termux:
mkdir -p ~/.ssh chmod 700 ~/.ssh nano ~/.ssh/authorized_keys -
Paste the public key, save and exit.
-
Set permissions:
chmod 600 ~/.ssh/authorized_keys
Final Step: Connect Using SSH Key
Use the same SSH command:
ssh -p 8022 username@192.168.x.x
If you set a passphrase, it will be prompted; otherwise, you will be logged in directly.
Summary Table
| Step | Command Example |
|---|---|
| Install OpenSSH | pkg install openssh |
| Start SSH server | sshd |
| Get IP address | ip address |
| Set password | passwd |
| SSH into device | ssh -p 8022 username@192.168.x.x |
| Generate SSH keys | ssh-keygen -t ed25519 |
| Copy SSH key automatically | ssh-copy-id -p 8022 username@192.168.x.x |
This setup allows your Android device to function like a lightweight and secure remote-access terminal.